package com.zerox.controller;

import com.alibaba.fastjson.JSON;
import com.zerox.domain.TableVo;
import com.zerox.pojo.SysDept;
import com.zerox.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/sysDept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping("/toDept")
    public String dept() {
        return "dept/sysDeptList";
    }

    @RequestMapping(value = "/list", produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String sysDept(Model model, HttpServletResponse response, Integer page, Integer limit) throws IOException {
        List<SysDept> deptList = sysDeptService.list((page - 1) * limit, limit);
        TableVo tableVo = new TableVo(0, 100, deptList, "");
        String json = JSON.toJSONString(tableVo);
        return json;
    }

    @GetMapping("/toAdd")
    public String add() {
        return "dept/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public String Add(Model model,SysDept sysDept){
        int result = sysDeptService.add(sysDept);
        if (result>0){
            return "true";
        }
        return "false";
    }

    @GetMapping("/view/{deptId}")
    public String view(Model model,@PathVariable Integer deptId){
        SysDept sysDept = sysDeptService.view(deptId);
        return "view";
    }

    @GetMapping("/update/{depeId}")
    @ResponseBody
    public String update(Model model,HttpServletResponse response,Integer deptId){
        SysDept sysDept = sysDeptService.view(deptId);
        model.addAttribute("sysDept",sysDept);
        return "dept/deptUpdate";
    }

    @PostMapping("/SysDeptUpdate")
    @ResponseBody
    public String update(Model model,SysDept sysDept){
        int result = sysDeptService.update(sysDept);
        if (result>0){
            return "true";
        }
        return "false";
    }

    @GetMapping("/sysDeptdelete/{deptId}")
    @ResponseBody
    public String delete(Model model,@PathVariable Integer deptId){
        int result = sysDeptService.delete(deptId);
        return result> 0 ? "true" : "false";
    }
}
